package xyz.csmcool.imroom.controller;

import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import xyz.csmcool.imroom.entity.vo.ExamineUserFriendVo;
import xyz.csmcool.imroom.entity.vo.UserFriendVo;
import xyz.csmcool.imroom.service.IUserFriendService;
import xyz.csmcool.imutil.dto.UserDto;
import xyz.csmcool.imutil.utils.JwtUtils;
import xyz.csmcool.imutil.utils.ResultTool;
import xyz.csmcool.imutil.webVo.MyPage;
import xyz.csmcool.imutil.webVo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author csmcool
 * @since 2022-08-15
 */
@RestController
@RequestMapping("/userFriend")
@RequiredArgsConstructor
public class UserFriendController {

    final IUserFriendService userFriendService;

    @GetMapping("/getMyFriendList/{limit}/{page}")
    public ResultVo<MyPage<UserDto>> getMyFriendList(@PathVariable("limit") Long limit, @PathVariable("page") Long page,
                                    HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<UserDto> myFriend = userFriendService.getMyFriend(page, limit, Integer.valueOf(uid));
        return ResultTool.success(myFriend);
    }

    @GetMapping("/getMyFriendList/{limit}/{page}/{name}")
    public ResultVo<MyPage<UserDto>> getMyFriendListByName(@PathVariable("limit") Long limit, @PathVariable("page") Long page,
                                                           HttpServletRequest request, @PathVariable("name") String name) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<UserDto> myFriend = userFriendService.getMyFriendByName(page, limit, Integer.valueOf(uid),name);
        return ResultTool.success(myFriend);
    }

    @GetMapping("/is-friend")
    public ResultVo<Map<String,Boolean>> checkIsFriend(@RequestParam("uid") Integer uid,
                                                       @RequestParam("fid") Integer fid) {
        Boolean friend = userFriendService.isFriend(uid, fid);
        Map<String,Boolean> map = Maps.newHashMapWithExpectedSize(1);
        map.put("result",friend);
        return ResultTool.success(map);
    }

    @GetMapping("/add-me-friend/{limit}/{page}")
    public ResultVo<MyPage<UserFriendVo>> getAddMeFriend(@PathVariable("limit") Long limit,
                                   @PathVariable("page") Long page,
                                   HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        MyPage<UserFriendVo> addMeFriend = userFriendService.getAddMeFriend(page, limit, Integer.valueOf(id));
        return ResultTool.success(addMeFriend);
    }

    @PostMapping("/examine-add-friend")
    public ResultVo examineAddFriend(HttpServletRequest request,
                                     @RequestBody ExamineUserFriendVo examineUserFriendVo) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        userFriendService.examineFriend(Integer.valueOf(id),examineUserFriendVo.getFid(),examineUserFriendVo.getIsPass());
        return ResultTool.success();
    }

    @GetMapping("/getUserFriendStatus")
    public ResultVo<Map<String,Integer>> getUserStatus(@RequestParam("uid") Integer uid,
                                                       @RequestParam("fid") Integer fid) {
        Integer integer = userFriendService.selectUserFriendStatus(uid, fid);
        Map<String,Integer> map = Maps.newHashMapWithExpectedSize(1);
        map.put("status",integer);
        return ResultTool.success(map);
    }

    @PostMapping("/add-friend")
    public ResultVo addFriend(HttpServletRequest request,
                              @RequestParam("fid") Integer fid) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        userFriendService.addFriend(Integer.valueOf(id),fid);
        return ResultTool.success();
    }
}
